package com.aiyun.research.democonfig.web;


import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
@RefreshScope
public class EnvResource {

    @Value("${configserver.name}")
    String configStatus;

    private final Environment environment;

    public EnvResource(Environment environment) {
        this.environment = environment;
    }

    @GetMapping("/config/env")
    public ResponseEntity<String> getEnvConfig() {
        String property = environment.getProperty("configserver.name");
        if (StringUtils.isEmpty(property)) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(property);
    }

    @GetMapping("/config/value")
    public ResponseEntity<String> getValConfig() {
        return ResponseEntity.ok(configStatus);
    }
}
